package com.neusoft.databus.core.repository.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.enums.ResourceWorkMode;
import com.neusoft.databus.common.model.QMetricGroup;
import com.neusoft.databus.common.model.QResource;
import com.neusoft.databus.common.model.Resource;
import com.neusoft.databus.core.repository.CustomizedResourceRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * description
 *
 * @author sunchf
 * @date 2018年12月17日 下午3:32:14
 */
public class CustomizedResourceRepositoryImpl extends AbstractRepository implements CustomizedResourceRepository {

    @Override
    public Page<Resource> search(final Pageable pageable, final Searchable searchable) {
        final QResource qResource = QResource.resource;
        final QMetricGroup qMetricGroup = QMetricGroup.metricGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Resource> query = this.query().selectFrom(qResource);
        if (searchable.hasKey("metricGroupCode")) {
            query.innerJoin(qResource.metricGroups, qMetricGroup)
                    .where(qMetricGroup.code.eq(searchable.getStrValue("metricGroupCode")));
        }
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<Resource> search(final Searchable searchable) {
        final QResource qResource = QResource.resource;
        final QMetricGroup qMetricGroup = QMetricGroup.metricGroup;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Resource> query = this.query().selectFrom(qResource);
        if (searchable.hasKey("metricGroupCode")) {
            query.innerJoin(qResource.metricGroups, qMetricGroup)
                    .where(qMetricGroup.code.eq(searchable.getStrValue("metricGroupCode")));
        }
        query.where(where);
        return query.fetch();
    }

    @Override
    public List<Resource> findCollectedResources() {
        final QResource qResource = QResource.resource;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(qResource.disabled.eq(false));
        where.and(qResource.gateway.isNotNull());
        where.and(qResource.gateway.disabled.eq(false));
        where.and(qResource.gateway.batched.eq(false));
        where.and(qResource.gateway.workMode.eq(ResourceWorkMode.PULL));
        where.and(qResource.metricGroups.isNotEmpty());
        final JPAQuery<Resource> query = this.query().selectFrom(qResource).where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return Resource.class;
    }

    @Override
    public Map<String, List<Long>> distribution() {
        final Map<String, List<Long>> statistic = new HashMap<>();
        final QResource qResource = QResource.resource;
        this.query().selectFrom(qResource).groupBy(qResource.gateway.code, qResource.gateway.name)
                .select(qResource.gateway.name, qResource.count()).fetch().stream().forEach(tuple -> {
                    final long count = tuple.get(qResource.count());
                    statistic.put(tuple.get(qResource.gateway.name), Arrays.asList(count));
                });
        return statistic;
    }

    @Override
    public long activeResourceCount() {
        final QResource qResource = QResource.resource;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(qResource.disabled.eq(false));
        where.and(qResource.gateway.disabled.eq(false));
        return this.query().selectFrom(qResource).where(where).fetchCount();
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QResource qResource = QResource.resource;
        where.and(super.containsStr(qResource.name, searchable, "name"));
        where.and(super.containsStr(qResource.code, searchable, "code"));
        where.and(super.containsStr(qResource.type, searchable, "type"));
        where.and(super.containsStr(qResource.associate, searchable, "associate"));
        where.and(super.containsStr(qResource.gateway.name, searchable, "gatewayName"));
        where.and(super.equalsBool(qResource.disabled, searchable, "disabled"));
        if (searchable.hasKey("gatewayWorking") && searchable.getBooleanValue("gatewayWorking")) {
            where.and(qResource.gateway.isNotNull());
            where.and(qResource.gateway.disabled.eq(false));
        }
        return where;
    }

}
